package com.example.perpetualcalendar.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;


public abstract class BaseFragment<VM extends ViewModel, B extends ViewDataBinding> extends Fragment {

    public VM mViewModel;
    public B viewBinding;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        mViewModel = new ViewModelProvider(this).get(setViewModel());
        viewBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), setLayout(), null, false);
        initView();
        return viewBinding.getRoot();
    }

    protected abstract void initView();

    /**
     * get ViewModel
     * @return 一个继承至 @see{@link ViewModel }的类
     */
    @NonNull
    protected abstract Class<VM> setViewModel();

    /**
     * get layoutId
     * @return .xml布局文件
     */
    @NonNull
    protected abstract int setLayout();

}
